home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / xlib / tvorder.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  244 lines

  1. /*
  2.  * (c) Copyright 1993-94, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software for
  6.  * any purpose and without fee is hereby granted, provided that the above
  7.  * copyright notice appear in all copies and that both the copyright notice
  8.  * and this permission notice appear in supporting documentation, and that
  9.  * the name of Silicon Graphics, Inc. not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.
  12.  *
  13.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  14.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  15.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  16.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  17.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  18.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  19.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  20.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  21.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  22.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  23.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  24.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  25.  *
  26.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
  27.  * Use, duplication, or disclosure by the Government is subject to
  28.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  29.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  30.  * clause at DFARS 252.227-7013 and/or in similar or successor
  31.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  32.  * Unpublished-- rights reserved under the copyright laws of the
  33.  * United States.  Contractor/manufacturer is Silicon Graphics,
  34.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  35.  *
  36.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  37.  */
  38. #include <GL/glx.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <X11/keysym.h>
  42.  
  43. static int attributes[] = {
  44.     GLX_RGBA,
  45.     GLX_RED_SIZE, 1,
  46.     GLX_GREEN_SIZE, 1,
  47.     GLX_BLUE_SIZE, 1,
  48.     None,
  49. };
  50.  
  51. int width = 200, height = 200;
  52.  
  53. static void DoDisplay(void)
  54. {
  55.     static GLfloat red[] = { 1, 0, 0, 1 };
  56.     static GLfloat green[] = { 0, 1, 0, 1 };
  57.     static GLfloat black[] = { 0, 0, 0, 1 };
  58.  
  59.     /* Init two sided lighting */
  60.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  61.     glMaterialfv(GL_FRONT, GL_EMISSION, red);
  62.     glMaterialfv(GL_BACK, GL_EMISSION, green);
  63.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, black);
  64.     glEnable(GL_LIGHTING);
  65.     glEnable(GL_LIGHT0);
  66.  
  67.     glMatrixMode(GL_PROJECTION);
  68.     glLoadIdentity();
  69.     glOrtho(-1, 1, -1, 1, 0, 1);
  70.     glMatrixMode(GL_MODELVIEW);
  71.     glLoadIdentity();
  72.  
  73.     glViewport(0, 0, width, height);
  74.  
  75.     /* Draw quad strip */
  76.     glClear(GL_COLOR_BUFFER_BIT);
  77.     glBegin(GL_QUAD_STRIP);
  78.     glVertex2f(-0.5,  0.5);
  79.     glVertex2f(-0.5, -0.5);
  80.     glVertex2f( 0.5,  0.5);
  81.     glVertex2f( 0.5, -0.5);
  82.     glVertex2f( 0.0,  0.5);
  83.     glVertex2f( 0.0, -0.5);
  84.     glEnd();
  85.     glFlush();
  86.  
  87.     {
  88.     GLfloat buf[10][4];
  89.     GLint i;
  90.  
  91.     /* Read back some of the pixels from the middle of the window */
  92.     glReadPixels(width/2 - 5, height/2, 10, 1, GL_RGBA, GL_FLOAT, buf);
  93.     printf("Pixels @ %d,%d:\n", width/2-5, height/2);
  94.     for (i = 0; i < 10; i++) {
  95.         printf("%g %g %g %g\n",
  96.            buf[i][0], buf[i][1], buf[i][2], buf[i][3]);
  97.     }
  98.     }
  99. }
  100.  
  101. static void Usage(void)
  102. {
  103.     printf("Usage: tvorder [-u]\n");
  104.     printf("   -u:  Render to unmapped window\n");
  105.     exit(-1);
  106. }
  107.  
  108. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  109. {
  110.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  111.     return GL_TRUE;
  112.     }
  113.     return GL_FALSE;
  114. }
  115.  
  116. static Bool WaitForUnmapNotify(Display *d, XEvent *e, char *arg)
  117. {
  118.     if ((e->type == UnmapNotify) && (e->xmap.window == (Window)arg)) {
  119.     return GL_TRUE;
  120.     }
  121.     return GL_FALSE;
  122. }
  123.  
  124. int main(long argc, char** argv)
  125. {
  126.     XVisualInfo *vi;
  127.     Display *dpy;
  128.     Colormap cmap;
  129.     Window window;
  130.     XSetWindowAttributes swa;
  131.     GLXContext cx;
  132.     XEvent event;
  133.     GLboolean needDisplay;
  134.     GLboolean unmapped = GL_FALSE;
  135.     int i;
  136.  
  137.     for (i = 1; i < argc; i++) {
  138.         if (argv[i][0] == '-') {
  139.             switch (argv[i][1]) {
  140.               case 'u':
  141.                 unmapped = GL_TRUE;
  142.                 break;
  143.               default:
  144.                 Usage();
  145.             }
  146.         } else {
  147.             Usage();
  148.         }
  149.     }
  150.  
  151.     dpy = XOpenDisplay(0);
  152.     if (!dpy) {
  153.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  154.     return -1;
  155.     }
  156.  
  157.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
  158.     if (!vi) {
  159.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  160.         getenv("DISPLAY"));
  161.     return -1;
  162.     }
  163.  
  164.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  165.                AllocNone);
  166.     swa.border_pixel = 0;
  167.     swa.colormap = cmap;
  168.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  169.     | KeyReleaseMask;
  170.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  171.                width, height,
  172.                0, vi->depth, InputOutput, vi->visual,
  173.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  174.     XSetStandardProperties(dpy, window, "tvorder", "tvorder", None,
  175.                argv, argc, NULL);
  176.     XSetWMColormapWindows(dpy, window, &window, 1);
  177.     XMapWindow(dpy, window);
  178.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  179.  
  180.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  181.     if (!glXMakeCurrent(dpy, window, cx)) {
  182.     fprintf(stderr, "Can't make window current to context\n");
  183.     return -1;
  184.     }
  185.  
  186.     if (unmapped) {
  187.         DoDisplay();
  188.         XUnmapWindow(dpy, window);
  189.         XIfEvent(dpy, &event, WaitForUnmapNotify, (char*)window);
  190.         DoDisplay();
  191.         return 0;
  192.     }
  193.  
  194.     needDisplay = GL_TRUE;
  195.     for (;;) {
  196.     do {
  197.         XNextEvent(dpy, &event);
  198.         switch (event.type) {
  199.           case Expose:
  200.         needDisplay = GL_TRUE;
  201.         break;
  202.           case ConfigureNotify:
  203.         width = event.xconfigure.width;
  204.         height = event.xconfigure.height;
  205.         needDisplay = GL_TRUE;
  206.         break;
  207.           case KeyPress:
  208.         {
  209.             char buf[100];
  210.             int rv;
  211.             KeySym ks;
  212.  
  213.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  214.             switch (ks) {
  215.               case XK_p:
  216.               case XK_P:
  217.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  218.             needDisplay = GL_TRUE;
  219.             break;
  220.               case XK_l:
  221.               case XK_L:
  222.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  223.             needDisplay = GL_TRUE;
  224.             break;
  225.               case XK_f:
  226.               case XK_F:
  227.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  228.             needDisplay = GL_TRUE;
  229.             break;
  230.               case XK_Escape:
  231.             return 0;
  232.             }
  233.         }
  234.         break;
  235.         }
  236.     } while (XPending(dpy) != 0);
  237.  
  238.     if (needDisplay) {
  239.         needDisplay = GL_FALSE;
  240.         DoDisplay();
  241.     }
  242.     }
  243. }
  244.